package cn.white.ymc.mvvmmaster.data.local.service

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.os.AsyncTask
import cn.white.ymc.mvvmmaster.data.bean.UserInfo
import cn.white.ymc.mvvmmaster.data.local.UserInfoDao
import cn.white.ymc.mvvmmaster.data.local.db.DBHelper

/**
 * service 实现
 *
 * @packageName: cn.white.ymc.mvvmmaster.data.local.service
 * @fileName: UserServiceImpl
 * @date: 2018/11/6  13:27
 * @author: ymc
 * @QQ:745612618
 */

object UserServiceImpl : UserService{


    private var userDao : UserInfoDao = DBHelper.getInstance().db.userInfoDao

    override fun queryByUsername(userName: String): LiveData<UserInfo> {
        return userDao.queryByUsername(userName)
    }

    override fun add(user: UserInfo): LiveData<Long> {
        val data = MutableLiveData<Long>()
        object : AsyncTask<Void, Void, Long>() {
            override fun doInBackground(vararg voids: Void): Long? {
                return userDao.add(user)
            }

            override fun onPostExecute(rowId: Long?) {
                data.setValue(rowId)
            }
        }.execute()
        return data
    }

}